package com.bjsxt.livegoods.comment.service.service.impl;

import com.bjsxt.livegoods.comment.service.service.CommentService;
import com.bjsxt.livegoods.dao.feedback.FeedbackDao;
import com.bjsxt.livegoods.entity.Feedback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    public Map<String, Object> findComments(String id, int page) {
        Query query=new Query();
        query.addCriteria(Criteria.where("productId").is(id));
        query.with(PageRequest.of(page,5));
        List<Feedback> feedbackList=feedbackDao.findFeedback(query);
        Query countQuery=new Query();
        countQuery.addCriteria(Criteria.where("productId").is(id));
        long total=feedbackDao.count(countQuery);
        //返回结果
        Map<String,Object> result=new HashMap<>();
        result.put("data",feedbackList);
        result.put("hasMore",(page+1)*5<total);
        return result;
    }
}
